#include "header.hpp"
using namespace std;

//basic example for condition variable
mutex lock1;
condition_variable cv;
bool ready = false;

void print_id(int id){
    unique_lock<mutex> lck(lock1);
    while(!ready){
        //Any thread step to here will release the lock and sleep
        cv.wait(lck);
    }
    cout << "Thread " << id << '\n';
}

void go(){
    unique_lock<mutex> lck(lock1);
    ready = true;
    cv.notify_all();
}

void test17(){
    thread threads[10];
    for(int i = 0; i < 10; ++i){
        threads[i] = thread(print_id, i);
    }

    cout << "10 threads ready to race...\n";
    this_thread::sleep_for(chrono::seconds(1));

    go();

    for(auto& t : threads){
        t.join();
    }
}
